﻿Imports Microsoft.WindowsMobile
Imports Microsoft.WindowsMobile.PocketOutlook
Imports Microsoft.WindowsMobile.PocketOutlook.MessageInterception
Imports System.Data.SqlServerCe

Public Class HomeForm

    Dim interceptor As MessageInterceptor

    Private Sub MenuItemExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemExit.Click
        Application.Exit()
    End Sub

    Private Sub MenuItemHide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemHide.Click
        Me.Hide()
    End Sub

    Private Sub EventsListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        EventView.Show()
        Me.Hide()
    End Sub

    Private Sub MenuItemAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemAddNew.Click
        EventAdd.Show()
        Me.Hide()
    End Sub

    Private Sub HomeForm_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
        'When we close the app, remove the listener
        RemoveHandler interceptor.MessageReceived, AddressOf SmsInterceptor_MessageReceived

    End Sub

    Private Sub HomeForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'SMSRSVPDataSet.Events' table. You can move, or remove it, as needed.
        Me.EventsTableAdapter.Fill(Me.SMSRSVPDataSet.Events)
        'TODO: This line of code loads data into the 'SMSRSVPDataSet.RSVPs' table. You can move, or remove it, as needed.
        Me.RSVPsTableAdapter.Fill(Me.SMSRSVPDataSet.RSVPs)


        'Set the messaging interceptor
        interceptor = New MessageInterceptor(InterceptionAction.NotifyAndDelete)
        interceptor.MessageCondition = New MessageCondition(MessageProperty.Body, MessagePropertyComparisonType.StartsWith, "SMSRSVP ", False)
        AddHandler interceptor.MessageReceived, AddressOf SmsInterceptor_MessageReceived

    End Sub

    Private Sub EventComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EventComboBox.SelectedIndexChanged
        Try
            Me.RSVPsTableAdapter.FillByCode(Me.SMSRSVPDataSet.RSVPs, CType(System.Convert.ChangeType(Me.EventComboBox.SelectedValue, GetType(Integer), Nothing), Integer))
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try
    End Sub


    Sub SmsInterceptor_MessageReceived(ByVal sender As Object, ByVal e As MessageInterceptorEventArgs)

        Dim msg As SmsMessage = e.Message

        If (Not msg.GetType.ToString = "Null") Then

            'Process the SMS text message 
            Dim bodyArr As String() = msg.Body.Split(" ")

            'Write Data to DB
            SaveSMS(msg.Received, msg.From.Address, msg.From.Name, msg.Body, bodyArr(1))

        End If
    End Sub

    Private Sub SaveSMS(ByVal rec As DateTime, ByVal from As String, ByVal fromName As String, ByVal body As String, ByVal EventCode As String)

        Dim cnn As SqlCeConnection = New SqlCeConnection("Data Source=\Program Files\SMSRSVP\SMSRSVP.sdf;")
        Dim cmd As SqlCeCommand = New SqlCeCommand
        cmd.Connection = cnn
        cnn.Open()

        cmd.CommandText = "INSERT INTO RSVPs([EventCode],[From]) VALUES('" + EventCode + "','" + fromName + "')"

        Try 'In case we already have this item we can't insert, so we will ingnore for simplicity

            cmd.ExecuteNonQuery()

            'MsgBox(fromName + " replied to your event.")

            Me.RSVPsTableAdapter.FillByCode(Me.SMSRSVPDataSet.RSVPs, CType(System.Convert.ChangeType(Me.EventComboBox.SelectedValue, GetType(Integer), Nothing), Integer))
        Catch ex As Exception
            'TODO: Notify user
        End Try

        'Clean up our connections
        cmd.Dispose()
        cnn.Dispose()

    End Sub

    'TODO: Need to provide option to stop listening to SMS

    ' Public Delegate Sub MessageInterceptorEventHandler(ByVal sender As Object, ByVal e As MessageInterceptorEventArgs)


    '    private Sub EnabledCheckbox_CheckStateChanged(object sender, EventArgs e)

    '    If (EnabledCheckbox.Checked) Then

    '            interceptor.MessageReceived += SmsInterceptor_MessageReceived;

    '    Else

    '            interceptor.MessageReceived -= SmsInterceptor_MessageReceived;
    '    End If

    'End Sub


End Class
